/**
 * @file    queue_event.c
 * @brief   队列
 * @author  Liuwei
 * @version 1.0.1
 * @date    2023-04-18
 * 
 * @copyright Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version   <th>Author   <th>Description
 * <tr><td>2023-04-18 <td>1.0.1     <td>Liuwei   <td>首次创建
 * </table>
 */
/* includes ------------------------------------------------------------------*/
#include "queue_event.h"
#include "mcu_type.h"
/* macro/define --------------------------------------------------------------*/

/* typedef -------------------------------------------------------------------*/

/* local functions -----------------------------------------------------------*/

/* static variables ----------------------------------------------------------*/

/* global variables ----------------------------------------------------------*/

/** ------------------------------------------------------------------------- *
  *                             Global function
 ** ------------------------------------------------------------------------- */

/**
 * @brief  队列初始化
 * @param  queue
 * @param  size
 * @param  buff
 */
void queue_init(queue_type *queue , uint16_t size , event_data_type *buff)
{
   queue->head = 0;
   queue->tail = 0;
   queue->free_size = size;
   queue->cap_size = size;
   queue->data_buf = buff;
}

uint8_t queue_is_empty(const queue_type queue)
{
   if(queue.free_size == queue.cap_size)
      return TRUE;
   else
      return FALSE;
}

uint8_t queue_is_full(const queue_type queue)
{
   if(0 == queue.free_size)
      return TRUE;
   else
      return FALSE;
}

void queue_in_fifo(queue_type *queue ,const event_data_type event_data)
{
   queue->data_buf[queue->head] = event_data;

   if(queue->free_size > 0)
      -- queue->free_size;

   if(++ queue->head >= queue->cap_size)
      queue->head = 0;
}

uint8_t queue_out_fifo(queue_type *queue , event_data_type *buff)
{
   if(queue->free_size < queue->cap_size)
   {
      *buff = *(queue->data_buf + queue->tail);
      ++ queue->free_size;

      if( ++ queue->tail >= queue->cap_size)
        queue->tail = 0;
	  
      return 1;
   }

   return 0;
}
/** ------------------------------------------------------------------------- *
  *                          Local function
 ** ------------------------------------------------------------------------- */
/*********************************@copyright***********************************/
